package com.geeksaga.service;

import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

import com.geeksaga.domain.User;

@Service
public class UserAuthenticationService implements AuthenticationManager {
    
    private User user;
    
    public Authentication authenticate(Authentication authentication, User user) throws DataAccessException {
        this.user = user;
        
        return authenticate(authentication);
    }
    
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        if(user != null) {
            return new UsernamePasswordAuthenticationToken(user, authentication.getCredentials(),
                user.getAuthorities());
        } else {
            throw new BadCredentialsException("Bad Credentials");
        }
    }
}